﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.SharePoint;
using Quix;
using System.IO;
using System.Xml;
using System.Xml.Linq;

namespace FindUniquePermissions
{
    class Program
    {
        [System.STAThreadAttribute()]
        [PreEmptive.Attributes.Setup(CustomEndpoint = "so-s.info/PreEmptive.Web.Services.Messaging/MessagingServiceV2.asmx")]
        [PreEmptive.Attributes.Teardown()]
        [PreEmptive.Attributes.Feature("Quix.FindUniquePermissions")]
        static void Main(string[] args)
        {
            if (args.Count() == 0)
            {
                PrintUsage();
            }
            else
            {
                try
                {
                    System.Collections.Hashtable tbl = Core.HashArgs(args);
                    if (!tbl.ContainsKey("-url"))
                    {
                        PrintUsage();
                    }
                    else
                    {
                        StartProcessing();
                        System.IO.StreamWriter sw = new System.IO.StreamWriter(Core.ConvertArgsToLogFile(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name + "-" + tbl["-url"].ToString()));
                        Core.Log("Checking for unique permission assignment in Site Collection: [" + tbl["-url"].ToString() + "]", sw, ConsoleColor.White);
                        //Get a site collection reference.
                        using (SPSite sps = new SPSite(tbl["-url"].ToString()))
                        {
                            //Process all sub webs in the site collection.
                            foreach (SPWeb spw in sps.AllWebs)
                            {
                                //Process all lists in each sub web.
                                foreach (SPList spl in spw.Lists)
                                {
                                    if (spl.HasUniqueRoleAssignments)
                                    {
                                        Core.Log("Site: [" + spw.Url + "] List: [" + spl.Title + "]", sw, ConsoleColor.White);
                                    }
                                    foreach (SPListItem fdr in spl.Folders)
                                    {
                                        if (fdr.HasUniqueRoleAssignments)
                                        {
                                            Core.Log("Site: [" + spw.Url + "] List: [" + spl.Title + "] Folder: [" + fdr.Url + "]", sw, ConsoleColor.White);
                                        }
                                    }
                                    foreach (SPListItem itm in spl.Items)
                                    {
                                        if (itm.HasUniqueRoleAssignments)
                                        {
                                            Core.Log("Site: [" + spw.Url + "] List: [" + spl.Title + "] Item: [" + itm.Url + "]", sw, ConsoleColor.White);
                                        }
                                    }
                                }
                            }
                        }
                        //Flush the log file.
                        sw.Flush();
                        StopProcessing();
                    }
                }
                catch (Exception ex)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(ex.Message);
                }
            }
        }

        static void PrintUsage()
        {
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine(@"Command line error.\n\nUsage:\n  " + Environment.GetCommandLineArgs()[0] + " -url <URL>");
            Console.WriteLine(@"    -url <URL> - URL of the target site e.g. http://www.crayveon.com/sites/Quix/Utilities");
            Console.WriteLine(@"\n    Example usage would be:");
            Console.WriteLine(@"    " + Environment.GetCommandLineArgs()[0] + " -url http://www.crayveon.com/sites/Quix/Utilities");
        }

        [PreEmptive.Attributes.Feature("Quix.FindUniquePermissions", EventType = PreEmptive.Attributes.FeatureEventTypes.Start)]
        static void StartProcessing()
        {
        }

        [PreEmptive.Attributes.Feature("Quix.FindUniquePermissions", EventType = PreEmptive.Attributes.FeatureEventTypes.Stop)]
        static void StopProcessing()
        {
        }
    }
}
